package com.ehouse.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {
	  
    private static final String TAG = "DatabaseHelper";
  
    public static final String DATABASE_NAME = "ehouse.db";
    public static final int DATABASE_VERSION = 1;

    // The underlying database
    @SuppressWarnings("unused")
	private SQLiteDatabase ehouseDB;
  
    public DatabaseHelper(Context context, String name,
                                    CursorFactory factory, int version) {
      super(context, name, factory, version);
    }
  
    @Override
    public void onCreate(SQLiteDatabase db) {
      db.execSQL(LightTable.DATABASE_CREATE);
      db.execSQL(TemperatureTable.DATABASE_CREATE);
      db.execSQL(IrrigationTable.DATABASE_CREATE);
      db.execSQL(WindowTable.DATABASE_CREATE);
      
      db.execSQL(LightScheduleTable.DATABASE_CREATE);
      db.execSQL(TemperatureScheduleTable.DATABASE_CREATE);
      db.execSQL(IrrigationScheduleTable.DATABASE_CREATE);
      
    }
  
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                 + newVersion + ", which will destroy all old data");
      //base
      db.execSQL("DROP TABLE IF EXISTS " + LightTable.NAME);
      db.execSQL("DROP TABLE IF EXISTS " + TemperatureTable.NAME);
      db.execSQL("DROP TABLE IF EXISTS " + IrrigationTable.NAME);
      db.execSQL("DROP TABLE IF EXISTS " + WindowTable.NAME);
      
      //schedules
      db.execSQL("DROP TABLE IF EXISTS " + LightScheduleTable.NAME);
      db.execSQL("DROP TABLE IF EXISTS " + TemperatureScheduleTable.NAME);
      db.execSQL("DROP TABLE IF EXISTS " + IrrigationScheduleTable.NAME);
      
      onCreate(db);
    }
  }